package cn.silver.module.authority.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.silver.framework.log.annotation.Log;
import cn.silver.framework.log.enums.OperateEnums;
import cn.silver.framework.mybatis.controller.DataController;
import cn.silver.framework.openapi.model.Result;
import cn.silver.module.authority.bean.RoleBean;
import cn.silver.module.authority.domain.SysRole;
import cn.silver.module.authority.param.RoleParam;
import cn.silver.module.authority.service.ISysRoleService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色信息
 *
 * @author Silver Zhou
 */
@Validated
@RestController
@RequiredArgsConstructor
@Tag(name = "权限管理-角色管理")
@RequestMapping("/system/role")
public class SysRoleController extends DataController<ISysRoleService, SysRole, RoleParam, RoleBean> {

    @Override
    @Resource
    public void setBaseService(ISysRoleService baseService) {
        this.baseService = baseService;
        this.setTitle("权限管理-角色管理");
        this.setCode("authority:role");
    }

    /**
     * 修改保存数据权限
     */
    @PutMapping("/dataScope")
    @SaCheckPermission("system:role:edit")
    @Log(title = "角色管理", operate = OperateEnums.ALTER_UPDATE)
    public Result<Void> dataScope(@RequestBody RoleBean role) {
        baseService.checkRoleAllowed(role);
        baseService.authDataScope(role);
//        baseService.checkRoleDataScope(role.getId());
        return Result.ok();
    }

    /**
     * 状态修改
     */
    @PutMapping("/changeStatus")
    @SaCheckPermission("system:role:edit")
    @Log(title = "角色管理", operate = OperateEnums.ALTER_UPDATE)
    public Result<Void> changeStatus(@RequestBody RoleBean role) {
        baseService.checkRoleAllowed(role);
//        baseService.checkRoleDataScope(role.getId());
        baseService.updateRoleStatus(role.getId(), role.getStatus());
        return Result.ok();
    }
}
